package dao.imp;

import java.util.List;


import org.hibernate.Session;

import org.hibernate.Transaction;
import org.hibernate.query.Query;

import dao.UserDao;
import entity.User;
import utils.HibernateUtils;

public class UserDaoImp implements UserDao {
	
	@Override
	public User getUserByUserName(String username) {
		Session session = HibernateUtils.getSession();
		Transaction transaction = session.beginTransaction();
		
		@SuppressWarnings("unchecked")
		Query<User> query = session.createQuery("from User where username = :username");
		
		query.setParameter("username", username);
		List<User> list = query.list();
		transaction.commit();
		if(list.isEmpty()){
			return null;
		}
		return list.get(0);
	}

}
